package com.adacms.test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;

import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.openyelp.data.apps.ObjectFactory;
import com.openyelp.data.entity.ShopCategory;
import com.openyelp.data.service.ShopCategoryService;
import com.spreada.utils.chinese.ZHConverter;

public class Apps {

	public static void main(String[] args) {

		String str = "你好世界";
		PinyinHelper.convertToPinyinString(str, ",",
				PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jiè
		PinyinHelper.convertToPinyinString(str, ",",
				PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4
		PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE); // ni,hao,shi,jie
		PinyinHelper.getShortPinyin(str); // nhsj
		// TODO Auto-generated method stub
		ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);

		String traditionalSrc = "有背光的機械式鍵盤";
		String simplified = converter.convert(traditionalSrc);
		System.out.println(simplified);

		// Or
		String simplifiedSrc = "简体字";
		String traditional = ZHConverter.convert(simplifiedSrc,
				ZHConverter.TRADITIONAL);
		System.out.println(traditional);

		String path = Apps.class.getResource("./category_tree.json").getFile();
		System.out.println(path);
		try {
			String json = FileUtils.readFileToString(new File(path));
			JsonParser parser = new JsonParser();
			JsonElement e = parser.parse(json);
			System.out.println(e.getAsJsonArray());
			JsonArray arrays = e.getAsJsonArray();
			s(arrays);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void s(JsonArray arrays, ShopCategory cs,int level) {
		Iterator<JsonElement> en = arrays.iterator();
		while (en.hasNext()) {
			JsonElement type = en.next();
			show(type, cs,level);
		}
	}

	private static void s(JsonArray arrays) {
		Iterator<JsonElement> en = arrays.iterator();
		while (en.hasNext()) {
			JsonElement type = en.next();
			show(type);
		}
	}

	private static void show(JsonElement type) {
		ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);

		System.out.println(type);
		System.out.println(type.getAsJsonObject().get("a"));
		System.out.println(type.getAsJsonObject().get("t"));
		System.out.println(converter.convert(type.getAsJsonObject().get("t")
				.getAsString()));
		ShopCategoryService userService = ObjectFactory.get().getBean(
				ShopCategoryService.class);
		ShopCategory m = new ShopCategory();
		String cname = converter.convert(type.getAsJsonObject().get("t")
				.getAsString());
		m.setCname(cname);
		String name = type.getAsJsonObject().get("a").getAsString();
		m.setName(name);
		m.setParent(ShopCategory.fromId(1));
		m.setPinyin(PinyinHelper.convertToPinyinString(cname, ",",
				PinyinFormat.WITHOUT_TONE));
		m.setPy(PinyinHelper.getShortPinyin(cname));
		m.setLevelinfo(1);
		ShopCategory cs = userService.save(m);

		JsonArray asa = type.getAsJsonObject().get("c").getAsJsonArray();
		if (asa != null) {
			s(asa, cs,1);
		}
	}

	private static void show(JsonElement type, ShopCategory cs,int level) {
		ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);
		level++;
		System.out.println(type);
		System.out.println(type.getAsJsonObject().get("a"));
		System.out.println(type.getAsJsonObject().get("t"));
		System.out.println(converter.convert(type.getAsJsonObject().get("t")
				.getAsString()));
		ShopCategoryService userService = ObjectFactory.get().getBean(
				ShopCategoryService.class);
		ShopCategory m = new ShopCategory();
		String cname = converter.convert(type.getAsJsonObject().get("t")
				.getAsString());
		m.setCname(cname);
		m.setPinyin(PinyinHelper.convertToPinyinString(cname, ",",
				PinyinFormat.WITHOUT_TONE));
		m.setPy(PinyinHelper.getShortPinyin(cname));

		String name = type.getAsJsonObject().get("a").getAsString();
		m.setName(name);
		m.setParent(cs);
		ShopCategory sss = userService.save(m);

		JsonArray asa = type.getAsJsonObject().get("c").getAsJsonArray();
		if (asa != null) {
			s(asa, sss,level);
		}
	}
}
